package com.qzsoft.zhjd.materiel.modules.materielsupplierdeviation.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qzsoft.jeemis.common.utils.ConvertDictUtils;
import com.qzsoft.zhjd.common.annotation.ZhjdDataFilter;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.materiel.modules.materielsupplierdeviation.dto.MaterielSupplierDeviationCountDto;
import com.qzsoft.zhjd.materiel.modules.materielsupplierdeviation.dto.MaterielSupplierDeviationItemDto;
import com.qzsoft.zhjd.materiel.modules.materielsupplierdeviation.dto.MaterielSupplierDeviationListDto;
import com.qzsoft.zhjd.materiel.modules.materielsupplierdeviation.dto.MaterielSupplierDeviationTopDto;
import com.qzsoft.zhjd.materiel.modules.materielsupplierdeviation.mapper.MaterielSupplierDeviationMapper;
import com.qzsoft.zhjd.materiel.modules.materielsupplierdeviation.service.MaterielSupplierDeviationService;

/**
 * @ClassName MaterielSupplierDeviationServiceImpl
 * @Description 供货商供货偏差业务接口实现
 * @author linhuawei
 * @since 2022-04-02
 */
@Service
public class MaterielSupplierDeviationServiceImpl implements MaterielSupplierDeviationService {

    @Autowired
    private MaterielSupplierDeviationMapper materielSupplierDeviationMapper;
    
    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public Map<String, MaterielSupplierDeviationCountDto> count(Map<String, Object> params) {
        // 获取本期统计信息
        MaterielSupplierDeviationCountDto current = materielSupplierDeviationMapper.count(params);

        // 获取累计统计信息，只保留权限数据
        Map<String, Object> totalParams = new HashMap<String, Object>();
        if (ObjectUtils.isNotEmpty(params.get(ZhjdDataFilter.DATA_FILTER_SQL))) {
            totalParams.put(ZhjdDataFilter.DATA_FILTER_SQL, params.get(ZhjdDataFilter.DATA_FILTER_SQL));
        }
        MaterielSupplierDeviationCountDto total = materielSupplierDeviationMapper.count(totalParams);

        // 返回数据
        Map<String, MaterielSupplierDeviationCountDto> result = new HashMap<String, MaterielSupplierDeviationCountDto>();
        result.put("current", current);
        result.put("total", total);
        return result;
    }

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public Map<String, List<MaterielSupplierDeviationTopDto>> top5(Map<String, Object> params) {
        // 获取本期统计信息
        List<MaterielSupplierDeviationTopDto> current = materielSupplierDeviationMapper.top5(params);

        // 获取累计统计信息，只保留权限数据
        Map<String, Object> totalParams = new HashMap<String, Object>();
        if (ObjectUtils.isNotEmpty(params.get(ZhjdDataFilter.DATA_FILTER_SQL))) {
            totalParams.put(ZhjdDataFilter.DATA_FILTER_SQL, params.get(ZhjdDataFilter.DATA_FILTER_SQL));
        }
        List<MaterielSupplierDeviationTopDto> total = materielSupplierDeviationMapper.top5(totalParams);

        // 返回数据
        Map<String, List<MaterielSupplierDeviationTopDto>> result = new HashMap<String, List<MaterielSupplierDeviationTopDto>>();
        result.put("current", current);
        result.put("total", total);
        return result;
    }
    
    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public IPage<MaterielSupplierDeviationListDto> pageForSupplier(Map<String, Object> params) {
        IPage<MaterielSupplierDeviationListDto> page = baseServiceComponent.getPage(params, MaterielSupplierDeviationListDto.class);
        return materielSupplierDeviationMapper.pageForSupplier(page, params);
    }
    
    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public IPage<MaterielSupplierDeviationItemDto> pageForItem(Map<String, Object> params) {
        IPage<MaterielSupplierDeviationItemDto> page = baseServiceComponent.getPage(params, MaterielSupplierDeviationItemDto.class);
        IPage<MaterielSupplierDeviationItemDto> dataPage = materielSupplierDeviationMapper.pageForItem(page, params);
        return ConvertDictUtils.formatDicDataPage(dataPage);
    }

}
